﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ScrumTime.Models.Story>" %>


<script type="text/C#" runat="server">
    private List<ScrumTime.Models.UsabilityFeature> AllUsabilityFeatures()
    {
        return ScrumTime.Helpers.UsabilityFeatures.All();
    }

    private bool isUsabilityFeatureSelect(ScrumTime.Models.UsabilityFeature u, 
            System.Data.Objects.DataClasses.EntityCollection<ScrumTime.Models.UsabilityFeature> ufs)
    {
        bool resultado = false;

        foreach (ScrumTime.Models.UsabilityFeature uf in ufs)
        {
            if (u.UsabilityFeatureId == uf.UsabilityFeatureId)
            {
                resultado = true;
                break;
            }
        }
        return resultado;
    }
    
    private ScrumTime.ViewModels.SprintCollectionViewModel getSprints(){
        ScrumTime.ViewModels.SprintCollectionViewModel sprintCollectionViewModel =
                    ScrumTime.ViewModels.SprintCollectionViewModel.BuildByStartDateDesc(
                       ScrumTime.Helpers.SessionHelper.GetCurrentProductId(Session)                       
                       );

        return sprintCollectionViewModel;
    }

    private String getIdsDependences(ScrumTime.Models.UsabilityFeature uf) {
        String resultado = "[";
        foreach (ScrumTime.Models.UsabilityFeatureDependence ufd in uf.UsabilityFeatureDependences)
        {
            if (resultado.Length > 1) 
                resultado += ",";

            resultado += ufd.UsabilityFeature1.UsabilityFeatureId.ToString();
        }
        resultado += "]";
        return resultado;
    }

    private String writeAddUsabilityFeatureDependence(ScrumTime.Models.Story Story, ScrumTime.Models.UsabilityFeature uf) {
        String resultado = String.Empty;
        if (uf.UsabilityFeatureDependences.Count > 0) {            
            String ids = getIdsDependences(uf);
            resultado = "addUsabilityFeatureDependence("+ Story.StoryId.ToString() + "," + ids + ", this );";
        }
        return resultado;
    }
    
</script>


<script>


    function getUsabilityFeaturesTexts(stroyId, ids, setCheck ) {
        var resultado = '';
        var i = 0;        
        for (i = 0; i < 200; i++) {
            var id = "#storyUsabilityFeature_" + stroyId + "_" + i;
            var valor = $(id).val();

            if (isNaN(valor))
                return resultado;

            for (j = 0; j < ids.length; j++) {
                
                if (ids[j] == valor) {
                    resultado += '\n-' + $(id).attr('nombre');

                    if (setCheck) {
                        $(id).attr('checked', true);
                    }
                }
            }
        }
        return resultado;
    }

    function addUsabilityFeatureDependence(storyId, ids, check) {
        if (!check.checked)
            return;

        var mensaje = 'This Usability Feature may depend on the following Usability Features:'
        mensaje += getUsabilityFeaturesTexts(storyId, ids, false);
        mensaje += '\nWant to add?';

        jConfirm(mensaje, 'Confirmation Dialog', function (r) {
            if (!r) return;
            getUsabilityFeaturesTexts(storyId, ids, true);
        });
    }

    function setCheckValue(ids) {
        alert('check the check boxes!!');
    }

    function setupcontrol_showStory_BasicData() {
        $("#show-story-basicdata-save").button();

        // set values
        $("#show-story-basicdata-idStory").val('<%:Model.StoryId%>');

        $("#show-story-basicdata-Priority").val('<%:Model.Priority%>');
        $("#show-story-basicdata-idName").val('<%:Model.UserDefinedId%>');
        $("#show-story-basicdata-Narrative").val('<%:Model.Narrative%>');
        $("#show-story-basicdata-Points").val('<%:Model.Points%>');
        $("#show-story-basicdata-Sprint").val('<%:Model.SprintId%>');
    }

    function SaveStoryBasicData() {
        pSaveStoryBasicData();
        //LoadTasks();
        //aqui cargar nuevamente todo el frame!        
        //var storyId = $('#show-story-basicdata-idStory').val();
       // show_story(storyId);
        return true;
    }

    function pSaveStoryBasicData() {
        var originalPriority = $("#show-story-basicdata-Priority").val();
//        if (originalPriority == 0) // handle the nulled/nonexisting originalValue
//            originalPriority = -9;
        var priority = $("#show-story-basicdata-Priority").val();
        var userDefinedId = $("#show-story-basicdata-idName").val();
        var narrative = $("#show-story-basicdata-Narrative").val();
        var points = $("#show-story-basicdata-Points").val();
        var sprintId = $('#show-story-basicdata-Sprint').val();
        var storyId = $('#show-story-basicdata-idStory').val();

        var usabilityFeatures = getUsabilityFeatures(storyId);

        $.post('/Story/Save',
            {
                storyId: storyId, priority: priority, sprintId: sprintId,
                userDefinedId: userDefinedId, narrative: narrative,
                originalPriority: originalPriority, points: points, usabilityFeatures: usabilityFeatures
            },
            function (data) {            
                if (verificarError(data)) return;
                //todo: aqui mostrar el show story aqui
                ShowForm(data);
            }
            );        
    }

    function LoadTasks() {
        refreshAcceptanceCriteriasList();
        refreshTasksList();
    }

</script>


<fieldset id="fs-show-story-basic-data" title="Basic Data">
    <input type="hidden" id="show-story-basicdata-idStory" />
    <table>
        <tr>
            <td>
                 <label for="show-story-basicdata-Priority">Priority:</label>
            </td>
            <td>
                <input id="show-story-basicdata-Priority"/>
            </td>
            <td>
                <label for="show-story-basicdata-idName">id/Name:</label>
            </td>
            <td>
                <input id="show-story-basicdata-idName"/>
            </td>
        </tr>       
        <tr>
            <td>
                <label for="narrative">Narrative:</label>
            </td>
            <td colspan="3">
                <textarea cols="80" rows="3" id="show-story-basicdata-Narrative" class="text ui-widget-content ui-corner-all"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <label for="show-story-basicdata-Points">Points:</label>
            </td>
            <td>
                <input id="show-story-basicdata-Points"/>
            </td>
            <td>
                <label for="show-story-basicdata-Sprint">Sprint:</label>
            </td>
            <td>                
                <select id="show-story-basicdata-Sprint">
                    <% foreach (ScrumTime.Models.Sprint sprint in getSprints().Sprints)
                       {
                    %>
                    <option value="<%:sprint.SprintId%>"><%:sprint.Name%> </option>
                    <%} %>
                </select>
            </td>
        </tr>
    </table>
		
        
		
</fieldset>

<fieldset id="fs-show-story-basic-data-uf" >
		<div style="width:100%">
                                Usability features:
                                <table style="width:100%;">
                                
                                <%
                                    int contador = 0;

                                    List<ScrumTime.Models.UsabilityFeature> all = AllUsabilityFeatures();
                                        
                                    foreach (ScrumTime.Models.UsabilityFeature uf in all )
                                    {
                                        
                                        if ((contador % 3) == 0) {
                                            
                                        %>
                                            <tr>
                                        <%                                        
                                        }
                                %>
                                        <td>
                                        <input type="checkbox" id="storyUsabilityFeature_<%:Model.StoryId%>_<%:contador%>"                                            
                                            onclick="<%:writeAddUsabilityFeatureDependence( Model, uf) %>"
                                            nombre ="<%:uf.Name%>" 
                                            value="<%:uf.UsabilityFeatureId%>" 
                                            <%  if ( isUsabilityFeatureSelect( uf, Model.UsabilityFeatures) ){ %>
                                                checked
                                            <%  }   %>
                                        >
                                           
                                            <%:uf.Name%>
                                        </input>
                                       
                                         </td>
                                 <%
                                         if ((contador % 3) == 2) {
                                             
                                        %>
                                            </tr>
                                        <%                                        
                                        }
                                        
                                        contador++;
                                    }
                                 %>

                                 <%
                                     int c = contador % 3;
                                     if ( c<= 2) {                                         
                                         %>
                                            </tr>
                                        <%                                                                                  
                                     }    
                                  %>

                                 </table>

                            </div>
</fieldset>

<div style="width:100%; text-align:right;">

<button id="show-story-basicdata-save" onclick="SaveStoryBasicData();" >Save</button>
</div>




<script>
    setupcontrol_showStory_BasicData();
</script>